package org.zn.plus.mybatis.sql.builder;

import java.util.Map;

import org.zn.commons.tree.Tree;
import org.zn.plus.mybatis.sql.builder.data.adapter.AbsrtactMappingFragmentAdapter;
import org.zn.plus.mybatis.sql.builder.data.adapter.AdapterMappingNode;
import org.zn.plus.mybatis.sql.builder.data.adapter.DataModelAdapter;
import org.zn.plus.mybatis.sql.builder.model.Fragment;

public class QueryFragmentFactory<D> implements AdapterFactory<D,Fragment>{
	
	private Map<String,Tree<String,AdapterMappingNode>> adapterMappingConfig;
	
	private Map<String,Class<? extends AbsrtactMappingFragmentAdapter<D>>> adapterConfig;  
	
	public Map<String, Tree<String, AdapterMappingNode>> getAdapterMappingConfig() {
		return adapterMappingConfig;
	}
	public void setAdapterMappingConfig(Map<String, Tree<String, AdapterMappingNode>> adapterMappingConfig) {
		this.adapterMappingConfig = adapterMappingConfig;
	}
	public Map<String, Class<? extends AbsrtactMappingFragmentAdapter<D>>> getAdapterConfig() {
		return adapterConfig;
	}
	public void setAdapterConfig(Map<String, Class<? extends AbsrtactMappingFragmentAdapter<D>>> adapterConfig) {
		this.adapterConfig = adapterConfig;
	}


	@Override
	public DataModelAdapter<D, Fragment> getModelAdapter(String name) {
		// TODO Auto-generated method stub
		
		Tree<String,AdapterMappingNode> tree= adapterMappingConfig.get(name);
		
		Class<? extends AbsrtactMappingFragmentAdapter<D>> clazz  =adapterConfig.get(name);
	
		AbsrtactMappingFragmentAdapter<D> af=null;

		try {
			af = clazz.newInstance();
			af.setAdapterMappingTree(tree);
		} catch (InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return af;
	}
		

	
}
